<?php
/**
 * Created by PhpStorm.
 * User: 墨鱼行
 * Signature: 好好学习，天天向上
 * Date: 18-10-3
 * Time: 下午3:27
 */

namespace app\api\validate;


use think\facade\Request;
use think\Validate;

class BaseValidate extends Validate
{
    /**
     * 校验参数
     * @return bool
     * @throws \app\lib\exception\ThrowError
     */
    public function goCheck()
    {
        if (Request::isPost())
        {
            $params = Request::post();
        }
        else
        {
            $params = Request::param();
        }
        $result = $this->check($params);

        if (!$result)
        {
            T(100000, [], 400, $this->error);
        }
        else
        {
            return $params;
        }
    }

    /**
     * 获取必要的参数，防止客户端传了额外的参数修改数据
     * @param $data
     * @return array
     */
    public function getDataByRule()
    {
        $keys = array_keys($this->rule);
        return Request::only($keys);
    }

    /**
     * 判断是否是正整数
     *
     * @param $value
     * @param string $rule
     * @param $data
     * @param $field
     * @return bool
     */
    protected function isPositiveInteger($value, $rule = '',
                                         $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 判断是否为空
     *
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return bool
     */
    protected function isNotEmpty($value, $rule = '',
                                  $data = '', $field = '')
    {
        if (empty($value))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}